package edu.ccut.saturn.workflow.agent;

import java.sql.Connection;
import java.util.HashMap;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.WorkFlowTools;

public class SetAgentPart extends DefaultComponent {
	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		HashMap<String,String> workFlowParamMap = (HashMap<String,String>)parameterContext.getParameterValue(0);
		String processIds = (String)parameterContext.getParameterValue(1);
		String activityIds = (String)parameterContext.getParameterValue(2);	
 		Connection conn = parameterContext.getConnection();	
		
 		SaturnData agentInst = WorkFlowTools.getAgentInstance();
		agentInst.set("agent_to", workFlowParamMap.get("agent_to"));
		agentInst.set("agent_from", workFlowParamMap.get("agent_from"));
		agentInst.set("agent_to_type", workFlowParamMap.get("agent_to_type"));
		agentInst.set("start_time", workFlowParamMap.get("start_time"));
		agentInst.set("end_time", workFlowParamMap.get("end_time"));
		agentInst.set("agent_type", "PART");
		agentInst.set("agent_reason", workFlowParamMap.get("agent_reason"));
		//插入代理表
		Operation.dataBase().insert(agentInst, conn);
	
		String agentId = agentInst.get("agent_id")+"";

		if(!"".equals(processIds)){
			String[] processList = processIds.split(",");
			for(int i=0;i<processList.length;i++){
				SaturnData agentitemInst = WorkFlowTools.getAgentitemInstance();	
				agentitemInst.set("itemid", processList[i]);
				agentitemInst.set("agentid",agentId);
				agentitemInst.set("itemtype","PROCESS");
				//插入代理item
				Operation.dataBase().insert(agentitemInst, conn);
			}
		}
		
		if(!"".equals(activityIds)){
			String[] activityList = activityIds.split(",");
			for(int i=0;i<activityList.length;i++){
				SaturnData agentitemInst = WorkFlowTools.getAgentitemInstance();	
			//注：actAndDef=activity_id+"|"+process_define_id
				agentitemInst.set("itemid", activityList[i]);
				agentitemInst.set("agentid",agentId);
				agentitemInst.set("itemtype","ACTIVITY");
				//插入代理item
				Operation.dataBase().insert(agentitemInst, conn);
			}
		}
	
	}
}
